package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;
import com.firefighting.util.StringUtil;

/**
 * Servlet implementation class SignIn
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/SignIn" })
public class SignIn extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userId=request.getParameter("userId");
	//	String time=request.getParameter("time");
		String longitude=request.getParameter("longitude");
		String latitude=request.getParameter("latitude");
		String task_id=request.getParameter("task_id");
		String sign_status=request.getParameter("sign_status");
		String distance=request.getParameter("distance");

		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		
		Connection connection = null;
		PreparedStatement ps = null;
	//	ResultSet rs = null;
	//	ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "insert into location(time,longitude,latitude,task_id,sign_status,distance,userId) values(?,?,?,?,?,?,?)";
			ps = connection.prepareStatement(sql);
			ps.setTimestamp(1, new java.sql.Timestamp(new Date().getTime()));
			ps.setString(2, longitude);
			ps.setString(3, latitude);
			ps.setString(4, task_id);
			ps.setString(5, sign_status);
			ps.setString(6, distance);
			ps.setString(7, userId);
			if (ps.executeUpdate() > 0) {
			// data=rs.getMetaData();
			//System.out.print(data.getColumnCount());
			// System.out.print(data.getColumnName(1));
			 
				result_jb.put("status", "true");
				result_jb.put("message", "获取位置成功");
			}else {
				result_jb.put("status", "false");
				result_jb.put("message", "获取位置失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
